﻿//注册
function Register() {
    CloseContentDiv(1);
}
//弹出登录
function Login() {
    CloseContentDiv(2);
}

//提交重置密码
function ResetPW() {
    var resetEmail = $.trim($("#resetEmail").val());
    if (resetEmail == "") {
        $("#resetMsg").html("邮箱不能为空！");
        return false;
    }
    var email = new RegExp("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    if (!email.test(resetEmail)) {
        $("#resetMsg").html("邮箱格式不正确！");
        return false;
    }
    else {
        ResetMsg();
    }

    $.ajax({
        type: "POST",
        url: "CourseInfo.ashx",
        dataType: "json",
        data: { action: "ForgetPassWord", loginName: resetEmail },
        success: function (result) {
            if (result == '1') {
                $("#resetMsg").html("该邮箱未注册！<a href='javascript:void(0);' class='blue' onclick='Register();'>立即注册</a>");
                return false;
            }
            else if (result == "0") {
                CloseContentDiv(4);
                SendTime(5, "sendTime", "");
            }
            else if (result == "-1") {
                alert("邮件服务器忙，请稳候再试");
            }
            else {
                alert("找回密码失败，请重试");
            }
        }
    });
    return false;
}
//重置密码
function ResetPassWord() {
    CloseContentDiv(3);
}

//定位到当前屏幕中间
function PitchDIV(pop) {
    $("#fullscreem").show();
    var left_w = ($(document).width() - pop.width()) / 2;
    var top_w = $('html').scrollTop() + ($(window).height() - pop.height()) / 2;
    pop.offset({ top: top_w, left: left_w });
    $("#fullscreem").css("height", $(document).height());
}

//关闭所有的Div
function CloseContentDiv(id) {
    $(".contentDiv").hide();
    $("#fullscreem").hide();
    ResetMsg();
    if (id != null && id != "" && id != "undefined") {
        id = id - 1;
        $(".contentDiv").eq(id).show();
        PitchDIV($(".contentDiv").eq(id));
    }
}

//重置提示消息
function ResetMsg() {
    $("#loginMsg").html("");
    $("#regEmailMsg").text("请输入有效Email地址");
    $("#regEmailMsg").removeClass("red").addClass("blue");
    $("#regPWMsg").text("密码请用6-16个字符");
    $("#regPWMsg").removeClass("red").addClass("blue");
    $("#regPWDMsg").text("");
    $("#regCheckCodeMsg").text("");
    $("#resetMsg").html("");
}

//登录
function GoLogin() {
    var loginName = $("#loginName").val();
    var passWord = $("#loginPW").val();
    if (loginName == "") {
        $("#loginMsg").html("登录名不能为空！");
        return false;
    }
    var email = new RegExp("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    if (!email.test(loginName)) {
        $("#loginMsg").html("用户名格式不正确，请重新输入！");
        return false;
    }
    if (passWord == "") {
        $("#loginMsg").html("密码不能为空！");
        return false;
    }
    $.ajax({
        type: "POST",
        url: "../Login.aspx",
        dataType: "json",
        data: { action: "GoLogin", loginName: loginName, passWord: passWord },
        success: function (result) {
            if (result[0] == "0") {
                alert("登录成功！");
                $("#userInfo").html("<span>欢迎您：" + result[1] + "</span>");
                $("#hid_userName").val(result[1]);
                $("#loginMsg").html("");
                CloseContentDiv();
                $("#logout").html("<a href='../user/mycourse.aspx'>我的课程</a> <a href='javascript:void(0);' onclick='Logout();'>退出登录</a>");
                $("#logout").hide();
            }
            else if (result[0] == "1") {
                $("#loginMsg").html("用户不存在，请先<a href='javascript:void(0);' class='blue' onclick='CloseContentDiv(1);'>注册</a>");
                return false;
            }
            else {
                $("#loginMsg").html("登录名或密码错误！");
                return false;
            }
        }
    });
}

//提交注册信息
function SubRegister() {
    var loginName = $.trim($("#regLoginName").val());
    var passWord = $.trim($("#regPW").val());
    var passWordD = $.trim($("#regPWD").val());
    var mobile = $.trim($("#regMobile").val());
    var checkCode = $.trim($("#regCheckCode").val());
    if (loginName == "") {
        $("#regEmailMsg").text("Email不能为空！");
        $("#regEmailMsg").removeClass("blue").addClass("red");
        return false;
    }
    else {
        ResetMsg();
    }
    var email = new RegExp("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    if (!email.test(loginName)) {
        $("#regEmailMsg").text("请输入有效Email地址");
        $("#regEmailMsg").removeClass("blue").addClass("red");
        return false;
    }
    else {
        ResetMsg();
    }
    if (passWord == "") {
        $("#regPWMsg").text("密码不能为空！");
        $("#regPWMsg").removeClass("blue").addClass("red");
        return false;
    }
    else {
        ResetMsg();
    }
    if (passWordD == "") {
        $("#regPWDMsg").text("确认密码不能为空！");
        return false;
    }
    else {
        ResetMsg();
    }
    if (passWord != passWordD) {
        $("#regPWDMsg").text("密码输入不一致！");
        return false;
    }
    else {
        ResetMsg();
    }
    if (checkCode == "") {
        $("#regCheckCodeMsg").text("验证码不能为空！");
        return false;
    }
    else {
        ResetMsg();
    }
    $.ajax({
        type: "POST",
        url: "../CodeCheck.aspx",
        dataType: "html",
        data: { checkcode: checkCode },
        success: function (data) {
            if (data == 1) {
                $.ajax({
                    type: "POST",
                    url: "CourseInfo.ashx",
                    dataType: "json",
                    data: { action: "Register", loginName: loginName, passWord: passWord, mobile: mobile },
                    success: function (result) {
                        if (result == '0') {
                            alert("注册成功，请登录！");
                            CloseContentDiv(2);
                        }
                        else if (result == '1') {
                            $("#regEmailMsg").text("该邮箱已被占用！");
                            $("#regEmailMsg").removeClass("blue").addClass("red");
                            return false;
                        }
                        else {
                            alert("注册失败！");
                            return false;
                        }
                    }
                });
            }
            else {
                $("#regCheckCodeMsg").text("验证码错误！");
                return false;
            }
        }
    });
}


